{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Infectious Diseases\n", "\n", "Consider a simple disease that follows these simple rules:\n", "\n", "* if you have it, then you always have it\n", "* if you come \"in contact\" with someone who has it, then you get it\n", "\n", "Sounds like zombies?\n", "\n", "What would happen? How can we model this world?\n", "\n", "## Simulation\n", "\n", "First, we must identify the objects we wish to model. Possibilities include: \n", "\n", "* people\n", "* world\n", "* disease\n", "\n", "First, let's consider what properties a person should have:\n", "\n", "* position in the world\n", "* whether it is carrying the disease or not\n", "* direction heading (vector, velocity on x and y)\n", "\n", "Also, what actions must a person be able to do:\n", "\n", "* move in the world\n", "* draw itself\n", "\n", "We might want to add other methods, but it needs to at least support those.\n", "\n", "## Person\n", "\n", "So, let's put together a person class. It might look something like:\n", "\n", "```java\n", "class Person {\n", " int x;\n", " int y;\n", " int vx;\n", " int vy;\n", " int carrying; // zero - not carrying disease; 1 - carrying disease\n", " \n", " Person(int x, int y) {\n", " this.x = int(x);\n", " this.y = int(y);\n", " this.randomizeDirection();\n", " this.carrying = 0;\n", " } \n", " ...\n", "}\n", "```\n", "\n", "Like the termites, instead of having them bounce off of the walls (like a ball simulation) we will instead have them \"wrap\" around (like a [torus](https://en.wikipedia.org/wiki/Torus)). For example, if a person goes too far to the left, it will wrap around to the right, and instantly appear there. Likewise, if a turtle goes below 0 or above height, then it wraps back around. Thus, we implement an infinite, repeating surface.\n", "\n", "This putting it all together might look like the following. \n", "\n", "Note:\n", "\n", "* people are represented by ovals. \n", "* people turn green when they are infected" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " Sketch #4:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #4 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "People [] person; // the array of people; we'll decide how many later\n", "int RADIUS = 5; // distance that we can sense a wood chip\n", "\n", "class Person {\n", " int x;\n", " int y;\n", " int vx;\n", " int vy;\n", " int carrying; // zero - not carrying disease; 1 - carrying disease\n", " \n", " Person(int x, int y) {\n", " this.x = int(x);\n", " this.y = int(y);\n", " this.randomizeDirection();\n", " this.carrying = 0;\n", " }\n", " \n", " void draw() {\n", " if (this.carrying == 0) {\n", " fill(128, 0, 0);\n", " } else {\n", " fill(0, 255, 0);\n", " }\n", " ellipse(this.x, this.y, 10, 10);\n", " }\n", " \n", " int get_x(int delta) {\n", " int dx = this.x + delta;\n", " if (dx >= width)\n", " dx -= width;\n", " if (dx < 0)\n", " dx += width;\n", " return dx;\n", " }\n", " \n", " int get_y(int delta) {\n", " int dy = this.y + delta;\n", " if (dy >= height)\n", " dy -= height;\n", " if (dy < 0)\n", " dy += height;\n", " return dy;\n", " }\n", " \n", " void step() {\n", " this.x = this.get_x(this.vx);\n", " this.y = this.get_y(this.vy);\n", " }\n", " \n", " void randomizeDirection() {\n", " this.vx = 0;\n", " this.vy = 0;\n", " while (this.vx == 0 && this.vy == 0) {\n", " this.vx = floor(random(3)) - 1;\n", " this.vy = floor(random(3)) - 1;\n", " }\n", " }\n", " \n", " float distance(Person person) {\n", " return sqrt( pow(this.x - person.x, 2) + pow(this.y - person.y, 2)); \n", " }\n", " \n", " void move() {\n", " if (this.carrying == 1) {\n", " if (random() < 1.0) { // 10% of the time, change directions\n", " this.randomizeDirection();\n", " }\n", " } else {\n", " if (random() < .01) { // 10% of the time, change directions\n", " this.randomizeDirection();\n", " }\n", " }\n", " this.step();\n", " // carrying a disease\n", " if (this.carrying == 1) { \n", " // find people within radius, and infect them\n", " // some percentage of time\n", " for (int p = 0; p < people.length; p++) {\n", " if (this.distance(people[p]) < RADIUS) {\n", " people[p].carrying = 1;\n", " }\n", " }\n", " } \n", " }\n", "}\n", "\n", "void setup() {\n", " size(500, 250); // the world\n", " people = new Person[500]; // start termites\n", " for (int i =0; i < people.length; i++) {\n", " people[i] = new Person(random(width), random(height));\n", " }\n", " // How many to infect initially?\n", " people[0].carrying = 1;\n", "}\n", "\n", "void draw() {\n", " background();\n", " noStroke();\n", " // Move the termites:\n", " for (int i =0; i < people.length; i++) {\n", " people[i].move();\n", " people[i].draw();\n", " } \n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Calysto Processing", "language": "processing", "name": "calysto_processing" }, "language_info": { "codemirror_mode": { "name": "text/x-java", "version": 2 }, "file_extension": ".java", "mimetype": "text/x-java", "name": "java" } }, "nbformat": 4, "nbformat_minor": 0 }